﻿using System;
using System.Collections.Generic;
using Microsoft.Practices.RecipeFramework;
using EnvDTE;
using System.ComponentModel.Design;
using Microsoft.Practices.RecipeFramework.Services;
using Microsoft.Practices.Common;
using ProjectBase.Guidance.Helpers;
using System.Collections.Specialized;

namespace ProjectBase.Guidance.ValueProviders
{
    public class SolutionProvider : ValueProvider, IAttributesConfigurable
    {
        private class Constants
        {
            private Constants()
            {
            }

            public const string Expression = "Expression";
        }

        #region IAttributesConfigurable Members

        void IAttributesConfigurable.Configure(StringDictionary attributes)
        {
            if (!attributes.ContainsKey(Constants.Expression))
            {
                throw new ArgumentException("Expression argument in attributes is mandatory");
            }
        }

        #endregion IAttributesConfigurable Members

        public override bool OnBeforeActions(object currentValue, out object newValue)
        {
            return Provide(out newValue);
            //return base.OnBeforeActions(currentValue, out newValue);
        }

        private bool Provide(out object newValue)
        {
            DTE dte = GetService<DTE>(true);

            IDictionaryService dictionaryService = GetService<IDictionaryService>(true);

            IConfigurationService configurationService = GetService<IConfigurationService>(true);

            Microsoft.Practices.RecipeFramework.Configuration.Argument argument = configurationService.CurrentRecipe.ArgumentsByName[Argument.Name];

            newValue = null;

            switch (Utils.AttributeByName(argument.ValueProvider.AnyAttr, Constants.Expression))
            {
                case "ProjectBase.Core":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.ProjectBase_Core);
                    break;

                case "ProjectBase.Data":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.ProjectBase_Data);
                    break;

                case "ProjectBase.Mvc":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.ProjectBase_Mvc);
                    break;

                case "ProjectBase.Tools":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.ProjectBase_Tools);
                    break;

                case "Project.Entity":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.Entity);
                    break;

                case "Project.Common":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.Common);
                    break;

                case "Project.Data":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.Data);
                    break;

                case "Project.Business":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.Business);
                    break;

                case "Project.Aop":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.Aop);
                    break;

                case "Project.Model":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.Mvc);
                    break;

                case "Project.Controller":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.Mvc);
                    break;

                case "Project.RestApi":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.RestApi);
                    break;

                case "Project.Mvc":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.Mvc);
                    break;

                case "Project.Web":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.Web);
                    break;
                //case "Project.UI":
                //    newValue = SolutionHelper.FindProject(dte, ProjectType.UI);
                //    break;
                case "Project.Tests":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.Tests);
                    break;

                case "Project.Tools":
                    newValue = SolutionHelper.FindProject(dte, ProjectType.Tools);
                    break;

                default: return false;
            }
            return true;
        }
    }
}